  
  
/*  
* Copyright @ 2017 com.iflysse.trains  
* xebest-erp 下午6:04:05  
* All right reserved.  
*  
*/  
  
package com.xebest.web.service.impl;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xebest.common.enums.erp.finance.VenPrepayStatusEnum;
import com.xebest.common.enums.erp.sys.VenDelEnum;
import com.xebest.util.seq.FinanceSeqUtil;
import com.xebest.web.controller.export.XeVenFinPrepayBillExDTO;
import com.xebest.web.controller.finance.XeVenFinPrepayBillController;
import com.xebest.web.form.XeVenFinPrepayBillForm;
import com.xebest.web.form.XeVenFinPrepayBillQueryForm;
import com.xebest.web.form.XeVenFinPrepayCheckForm;
import com.xebest.web.mapper.read.XeVenFinPrepayBillReadMapper;
import com.xebest.web.mapper.read.XeVenPurchSupInfoReadMapper;
import com.xebest.web.mapper.write.XeVenFinPrepayBillWriteMapper;
import com.xebest.web.mapper.write.XeVenFinPrepayPurchaseWriteMapper;
import com.xebest.web.mapper.write.XeVenFinPrepaySettleWriteMapper;
import com.xebest.web.model.XeVenFinPrepayPurchase;
import com.xebest.web.model.XeVenFinPrepaySettle;
import com.xebest.web.service.XeVenFinPrepayBillService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.SuppliersForPrepayView;
import com.xebest.web.view.XeVenFinPrepayBillPrintView;
import com.xebest.web.view.XeVenFinPrepayBillView;

  
/**  
 * @desc: xebest-erp  
 * @author: 杜春静  
 * @createTime: 2017年12月4日 下午6:04:05  
 * @history:  
 * @version: v1.0    
*/  
@Service  
public class XeVenFinPrepayBillServiceImpl implements XeVenFinPrepayBillService {
	
	private static final Logger log = LoggerFactory.getLogger(XeVenFinPrepayBillController.class);// 日志记录类
	
	@Autowired
	XeVenFinPrepayBillWriteMapper prepayBillWriteMapper;//预付单写mapper
	
	@Autowired
	XeVenFinPrepayPurchaseWriteMapper prepayPurchWriteMapper;//预付单采购单关联表Mapper
	
	@Autowired
	XeVenFinPrepayBillReadMapper prepayBillReadMapper;//预付单读mapper
	@Autowired
	XeVenPurchSupInfoReadMapper purchSupInfoReadMapper;//供货商读mapper
	@Autowired
	XeVenFinPrepaySettleWriteMapper prepaySettleWriteMapper;//预付单结账单关联表Mapper
	/* 
	 * 添加预付单 
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#addPrepayBill(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillForm)  
	*/  
	@Override
	@Transactional(value = "writeTransactionManager")
	public int addPrepayBill(ErpUserInfo userInfo,
			XeVenFinPrepayBillForm prepayBillForm) {
		int result = 0;
		
		  if(null != userInfo){
			  prepayBillForm.setUserCode(userInfo.getUserCode());
			  prepayBillForm.setCreateUser(userInfo.getLoginName());
			  prepayBillForm.setModifyUser(userInfo.getLoginName());
		  
			  //添加预付单
			  result = prepayBillWriteMapper.insertPrepayBill(prepayBillForm);
			  if(result > 0){
				  String[] purchOrders = prepayBillForm.getPurchaseCodes();
				 //添加预付单采购单关联表
				  result = insertPrepayRelationPurch(purchOrders,userInfo,prepayBillForm.getPrepayBillNo());
			  }
		  }
		return result;  
		
	}
	/**
	 * 添加预付单采购单关联表
	 * @author: 杜春静  
	 * @createTime: 2017年12月5日 下午2:45:44  
	 * @history:  
	 * @param purchOrders
	 * @param userInfo
	 * @return int
	 */
	@Transactional(value = "writeTransactionManager")
	public int insertPrepayRelationPurch(String[] purchOrders, ErpUserInfo userInfo,String prepayBillNo){
		int result = 0;
		if(purchOrders.length > 0 ){
			 Map<String, Object> paramMap = new HashMap<String, Object>();
			 paramMap.put("userCode", userInfo.getUserCode());
			 paramMap.put("prepayBillNo", prepayBillNo);
			 prepayPurchWriteMapper.deleteRelationPurchByPrepayNo(paramMap);
			 List<XeVenFinPrepayPurchase> lists = new ArrayList<XeVenFinPrepayPurchase>();
			
			 for(String purchOrder : purchOrders){
				 XeVenFinPrepayPurchase prepayPurch = new XeVenFinPrepayPurchase();
				 prepayPurch.setUserCode(userInfo.getUserCode());
				 prepayPurch.setPrepayBillNo(prepayBillNo);
				 prepayPurch.setPurchaseCode(purchOrder);
				 prepayPurch.setCreateUser(userInfo.getLoginName());
				 prepayPurch.setModifyUser(userInfo.getLoginName());
				 lists.add(prepayPurch);
			 }
			 result = prepayPurchWriteMapper.batchInsertPrepayPurch(lists);
		 }
		return result;
	}
	  
	/*  
	 * 修改预付单
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#editPrepayBill(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillForm)  
	*/  
	@Override
	@Transactional(value = "writeTransactionManager")
	public int editPrepayBill(ErpUserInfo userInfo,
			XeVenFinPrepayBillForm prepayBillForm) {
		int result = 0;
		  if(null != userInfo){
			  prepayBillForm.setUserCode(userInfo.getUserCode());
			  prepayBillForm.setModifyUser(userInfo.getLoginName());
			  prepayBillForm.setPrepayStatus(VenPrepayStatusEnum.CHECK_PENDING.getValue());
			  //修改预付单
			  result = prepayBillWriteMapper.updatePrepayBillByCode(prepayBillForm);
			  if(result > 0 && prepayBillForm.getPurchaseCodes() != null){
				  String[] purchOrders = prepayBillForm.getPurchaseCodes();
				 //添加预付单采购单关联表
				  result = insertPrepayRelationPurch(purchOrders,userInfo,prepayBillForm.getPrepayBillNo());
			  }
		  }
		return result;
	}
	  
	/* 
	 * 统计预付单的数量 
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#countBillByParam(java.util.Map)  
	*/  
	
	@Override
	public int countBillByParam(Map<String, Object> param) {
		  
		  
		return prepayBillWriteMapper.countBillByParam(param);  
		
	}
	  
	/*  
	 * 删除预付单
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#delPrepayBill(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayCheckForm)  
	*/  
	@Override
	@Transactional(value = "writeTransactionManager")
	public int delPrepayBill(ErpUserInfo userInfo, XeVenFinPrepayCheckForm form) {
		int result = 0; 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		 paramMap.put("userCode", userInfo.getUserCode());
		 paramMap.put("delFlag", VenDelEnum.YES.getValue());
		 paramMap.put("prepayBills", form.getPrepayBills());
		 paramMap.put("modifyUser", userInfo.getLoginName());
		 log.info("更新预付单为删除状态");
		 result = prepayBillWriteMapper.updatePrepayBillByCollection(paramMap);
		 if(result > 0){
			 result = prepayPurchWriteMapper.updateRelationPurchToDel(paramMap);
		 }
		 return result;  
		
	}
	  
	/* 
	 * 批量审核预付单通过 
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#checkPrepayBillPass(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayCheckForm)  
	*/  
	@Override
	public int checkPrepayBillPass(ErpUserInfo userInfo,
			XeVenFinPrepayCheckForm form) {
		int result = 0; 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		 paramMap.put("userCode", userInfo.getUserCode());
		 paramMap.put("purchCheckTime", "1");
		 paramMap.put("prepayStatus", VenPrepayStatusEnum.FINANCE_CHECK_PENDING.getValue());
		 paramMap.put("purchCheckUserName", userInfo.getUserName());
		 paramMap.put("prepayBills", form.getPrepayBills());
		 paramMap.put("modifyUser", userInfo.getLoginName());
		 log.info("审核预付单为通过");
		 result = prepayBillWriteMapper.updatePrepayBillByCollection(paramMap);
		 return result; 
		
	}
	  
	/*  
	 * 批量审核预付单不通过 
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#checkPrepayBillRefuse(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayCheckForm)  
	*/  
	@Override
	public int checkPrepayBillRefuse(ErpUserInfo userInfo, XeVenFinPrepayCheckForm form) {
		  
		int result = 0; 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		 paramMap.put("userCode", userInfo.getUserCode());
		 paramMap.put("purchCheckTime", "1");
		 paramMap.put("prepayStatus", VenPrepayStatusEnum.CHECK_REFUSE.getValue());
		 paramMap.put("purchCheckUserName", userInfo.getUserName());
		 paramMap.put("purchCheckRemark", form.getRefuseReason());
		 paramMap.put("prepayBills", form.getPrepayBills());
		 paramMap.put("modifyUser", userInfo.getLoginName());
		 log.info("批量审核预付单不通过 ");
		 result = prepayBillWriteMapper.updatePrepayBillByCollection(paramMap);
		 return result; 
		
	}
	  
	/* 
	 * 财务部批量审核预付单通过  
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#finCheckPrepayBillPass(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayCheckForm)  
	*/  
	
	@Override
	public int finCheckPrepayBillPass(ErpUserInfo userInfo,
			XeVenFinPrepayCheckForm form) {
		  
		int result = 0; 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		 paramMap.put("userCode", userInfo.getUserCode());
		 paramMap.put("finCheckTime", "1");
		 paramMap.put("prepayStatus", VenPrepayStatusEnum.PREPAYING.getValue());
		 paramMap.put("finCheckUserName", userInfo.getUserName());
		 paramMap.put("finCheckRemark", form.getRefuseReason());
		 paramMap.put("prepayBills", form.getPrepayBills());
		 paramMap.put("modifyUser", userInfo.getLoginName());
		 log.info("财务部批量审核预付单通过  ");
		 result = prepayBillWriteMapper.updatePrepayBillByCollection(paramMap);
		 return result; 
		
	}
	  
	/*  
	 * 财务部批量审核预付单不通过  
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#finCheckPrepayBillRefuse(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayCheckForm)  
	*/  
	
	@Override
	public int finCheckPrepayBillRefuse(ErpUserInfo userInfo,
			XeVenFinPrepayCheckForm form) {
		  
		int result = 0; 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userCode", userInfo.getUserCode());
		 paramMap.put("finCheckTime", "1");
		 paramMap.put("prepayStatus", VenPrepayStatusEnum.FINANCE_CHECK_REFUSE.getValue());
		 paramMap.put("finCheckUserName", userInfo.getUserName());
		 paramMap.put("finCheckRemark", form.getRefuseReason());
		 paramMap.put("prepayBills", form.getPrepayBills());
		 paramMap.put("modifyUser", userInfo.getLoginName());
		 log.info("财务部批量审核预付单不通过 ");
		 result = prepayBillWriteMapper.updatePrepayBillByCollection(paramMap);
		 return result;
	}
	  
	/*  
	 * 查询预付单列表
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#queryPrepayBill(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillQueryForm)  
	*/  
	
	@Override
	public List<XeVenFinPrepayBillView> queryPrepayBillList(ErpUserInfo userInfo,
			XeVenFinPrepayBillQueryForm form) {
		  
		List<XeVenFinPrepayBillView> prepayBillList = new ArrayList<XeVenFinPrepayBillView>();
		if(userInfo != null ){
			form.setUserCode(userInfo.getUserCode());
		}
		log.info("查询预付单列表");
		prepayBillList = prepayBillReadMapper.queryPrepayBillList(form);
		return prepayBillList;  
		
	}
	  
	/* 查询总的预付款金额和单数 
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#queryTotalPrepayAmount(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillQueryForm)  
	*/  
	
	@Override
	public Map<String, Object> queryTotalPrepayAmount(ErpUserInfo userInfo,
			XeVenFinPrepayBillQueryForm form) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		form.setUserCode(userInfo.getUserCode());
		log.info("查询总的预付款金额和单数");
		resultMap = prepayBillReadMapper.queryTotalPrepayAmount(form);
		return resultMap; 
	}
	  
	/* 
	 * 财务部批量付款预付单 
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#finPayPrepayBill(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillQueryForm)  
	*/  
	@Override
	public int finPayPrepayBill(ErpUserInfo userInfo,
			XeVenFinPrepayCheckForm form) {
		  
		int result = 0; 
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userCode", userInfo.getUserCode());
		paramMap.put("payRemark", form.getPayRemark());
		paramMap.put("prepayDate", form.getPrepayDate());
		 paramMap.put("prepayStatus", VenPrepayStatusEnum.PREPAY_FINISH.getValue());
		 paramMap.put("supplierCode", form.getSupplierCode());
		 paramMap.put("modifyUser", userInfo.getLoginName());
		 paramMap.put("prepayBills", form.getPrepayBills());
		 log.info("财务部批量付款预付单");
		 result = prepayBillWriteMapper.updatePrepayBillByCollection(paramMap);
		 return result; 
		
	}
	  
	/*  
	 * 根据供货商查询预付单可关联的采购单号
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#querySupplierPurchase(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillQueryForm)  
	*/  
	
	@Override
	public List<String> querySupplierPurchase(ErpUserInfo userInfo,
			XeVenFinPrepayBillQueryForm form) {
		  
		List<String> purchOrders = new ArrayList<String>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("supplierCode", form.getSupplierCode());
		map.put("userCode", userInfo.getUserCode());
		map.put("prepayBillNo", form.getPrepayBillNo());
		log.info("根据供货商{}查询预付单可关联的采购单号",form.getSupplierCode());
		purchOrders = prepayPurchWriteMapper.querySupplierPurchase(map);
		return purchOrders;  
		
	}
	  
	/*
	 * 查询预付单可以进行付款的供货商code  
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#querySuppliersForPrepay(com.xebest.web.view.ErpUserInfo)  
	*/  
	
	@Override
	public List<SuppliersForPrepayView> querySuppliersForPrepay(
			ErpUserInfo userInfo,XeVenFinPrepayBillQueryForm form) {
		List<SuppliersForPrepayView> suppliers = new ArrayList<SuppliersForPrepayView>();  
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userCode", userInfo.getUserCode());
		map.put("supplierName", form.getSupplierName());
		suppliers = purchSupInfoReadMapper.querySuppliersForPrepay(map);
		return suppliers;  
		
	}
	  
	/*  
	 * 查询预付单打印数据
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#queryPrepayBill(com.xebest.web.view.ErpUserInfo, com.xebest.web.form.XeVenFinPrepayBillQueryForm)  
	*/  
	
	@Override
	public XeVenFinPrepayBillPrintView queryPrepayBill(ErpUserInfo userInfo,
			XeVenFinPrepayBillQueryForm form) {
		
		XeVenFinPrepayBillPrintView prepayBillPrintData = new XeVenFinPrepayBillPrintView();
		form.setUserCode(userInfo.getUserCode());
		log.info("查询预付单打印数据");
		prepayBillPrintData = prepayBillReadMapper.queryPrepayBill(form);
		return prepayBillPrintData;  
		
	}
	  
	/*  
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#addRelationSettlePrepay(java.util.List)  
	*/  
	
	@Override
	@Transactional(value = "writeTransactionManager")
	public int addRelationSettlePrepay(List<XeVenFinPrepaySettle> spList) {
		int result = 0;
		if(spList != null && spList.size() > 0){
			XeVenFinPrepaySettle prepaySettle = spList.get(0); 
			 String  relationSatementNo = prepaySettle.getRelationSatementNo();
//			 String userCode = prepaySettle.getUserCode();
			 Map<String, Object> param = new HashMap<String, Object>();
			 param.put("relationSatementNo", relationSatementNo);
//			 param.put("userCode", userCode);
			 log.info("删除预付单和结账的关联关系");
			 this.prepaySettleWriteMapper.deleteRelationSettleBySateno(param);
			 log.info("批量添加预付单和结账单的关联关系");
			 result = prepaySettleWriteMapper.batchInsertRelationSettle(spList);
		}
		
		return result;  
		
	}
	  
	/*  
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#relieveRelationSettlePrepay(java.lang.String, java.lang.String[], java.lang.String)  
	*/  
	
	@Override
	public int relieveRelationSettlePrepay(String modifyUser,
			String[] satementNos) {
		 int result = 0; 
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("modifyUser", modifyUser);
		param.put("satementNos", satementNos);
		result = prepaySettleWriteMapper.updateRelationSettleToDel(param);
		return result;  
		
	}
	  
	/*  
	 *(non-Javadoc)  
	 * @see com.xebest.web.service.XeVenFinPrepayBillService#exportPrepayList(com.xebest.web.form.XeVenFinPrepayBillQueryForm)  
	*/  
	
	@Override
	public List<XeVenFinPrepayBillExDTO> exportPrepayList(
			ErpUserInfo userInfo,XeVenFinPrepayBillQueryForm form) {
		  
		List<XeVenFinPrepayBillExDTO> prepayBillList = new ArrayList<XeVenFinPrepayBillExDTO>();
		if(userInfo != null ){
			form.setUserCode(userInfo.getUserCode());
		}
		log.info("查询要导出的预付单列表");
		prepayBillList = prepayBillReadMapper.queryExPrepayList(form);
		return prepayBillList;    
		
	}
	
	
}
  
